package com.huo.filestransport.controller;

import com.huo.annotation.TaskLogAnnotation;
import com.huo.common.enumtype.FileTypeEnum;
import com.huo.common.enumtype.R;
import com.huo.filestransport.service.FtpService;
import com.huo.filestransport.vo.FtpFileSimpleInfo;
import com.huo.filestransport.vo.FtpServerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/transport")
public class TransportController {

	@Autowired
	private FtpService transportService;

	@Autowired
	private FtpServerInfo ftpServerInfo;

	@ResponseBody
	@RequestMapping("")
	@TaskLogAnnotation
	public R aliveServer() {
		return R.ok();
	}

	@ResponseBody
	@RequestMapping("/clearFtpFileInfo")
	public R clearFtpFileInfo() {
		transportService.clearFtpFileInfo();
		return R.ok();
	}

	@ResponseBody
	@RequestMapping("/checkFtpFile")
	public R checkFtpFile() {
		return transportService.checkFtpFile(transportService.makeFtpDirectory(FileTypeEnum.DOWNLOAD));
	}
	
	@ResponseBody
	@RequestMapping("/checkFtpFileByDB")
	public R checkFtpFileByDB() {
		return transportService.checkFtpFileByDB();
	}

	@ResponseBody
	@RequestMapping("/downloadFtpFileByFtpDirectory")
	public R downloadFtpFileByFtpDirectory(String ftpDirectory) {
		return transportService.downloadFtpFileByFtpDirectory(ftpDirectory, ftpServerInfo.getLocalDirectory());
	}

	@ResponseBody
	@RequestMapping("/downloadFtpFileByDB")
	public R downloadFtpFileByDB() {
		return transportService.downloadFtpFileByDB();
	}

	@ResponseBody
	@RequestMapping("/downloadFtpFileByFtpPath")
	public R downloadFtpFileByFtpPath(String ftpPath) {
		if (ftpPath != null && ftpPath != "") {
			return transportService.downloadFtpFielByFtpPath(ftpPath, ftpServerInfo.getLocalDirectory());
		} else {
			return R.error("请输入正确的ftp路径");
		}
	}
	
	@ResponseBody
	@RequestMapping("/downloadFtpFileAndUploadByFtpPath")
	public R downloadFtpFileAndUploadByFtpPath(String ftpPath) {
		if (ftpPath != null && ftpPath != "") {
			R re = transportService.downloadFtpFielByFtpPath(ftpPath, ftpServerInfo.getLocalDirectory());
			if(!R.checkOK(re)) {
				return re;
			}
			R r = transportService.uploadFileByLocalPath(transportService.makeFtpDirectory(FileTypeEnum.UPLOAD),
					ftpServerInfo.getLocalDirectory()+ftpPath);
			if(!R.checkOK(r)) {
				return r;
			}
		} else {
			return R.error("请输入正确的ftp路径");
		}
		return R.ok();
	}
	
	@ResponseBody
	@RequestMapping("/downloadFtpFileAndUploadByFtpDirectory")
	public R downloadFtpFileAndUploadByFtpDirectory(String ftpDirectory) {
		if (ftpDirectory != null && ftpDirectory != "") {
			R re = transportService.downloadFtpFileByFtpDirectory(ftpDirectory, ftpServerInfo.getLocalDirectory());
			if(!R.checkOK(re)) {
				return re;
			}
			R r = transportService.uploadFileByLocalDirectory(transportService.makeFtpDirectory(FileTypeEnum.UPLOAD),
					transportService.makeLocalFileDirectory());
			if(!R.checkOK(r)) {
				return r;
			}
		} else {
			return R.error("请输入正确的ftp路径");
		}
		return R.ok();
	}

	@ResponseBody
	@RequestMapping("/downloadFtpFileByDefinition")
	public R downloadFtpFileByDefinition() {
		if (ftpServerInfo != null && ftpServerInfo.getDefinitionFileName() != null
				&& ftpServerInfo.getDefinitionFileName().size() > 0) {
			return transportService.downloadFtpFileByDefinition(transportService.makeFtpPathByDifiniton(),
					ftpServerInfo.getLocalDirectory());
		} else {
			return R.error("配置文件未正确配置");
		}
	}

	@ResponseBody
	@RequestMapping("/downloadFtpFileByDBAndThread")
	public R downloadFtpFileByDBAndThread() {
		return transportService.downloadFtpFileByDBAndThread(ftpServerInfo.getFtpDirectory(),
				ftpServerInfo.getLocalDirectory(), 3);
	}

	@ResponseBody
	@RequestMapping("/getFtpFileListByDirectory")
	public List<FtpFileSimpleInfo> getFtpFileDirectory(String ftpDirectory) {

		return transportService.readFTPFileDirectory(ftpDirectory);
		/*if (ftpDirectory != null && ftpDirectory != "") {
			return transportService.readFTPFileDirectory(ftpDirectory);
		} else {
			return new ArrayList<FtpFileSimpleInfo>();
		}*/
	}

	@ResponseBody
	@RequestMapping("/uploadFileByLocalDirectory")
	public R uploadFileByLocalDirectory(String localFileDirectory) {
		return transportService.uploadFileByLocalDirectory(transportService.makeFtpDirectory(FileTypeEnum.UPLOAD),
				localFileDirectory);
	}

	@ResponseBody
	@RequestMapping("/uploadFileByResources")
	public R uploadFileByResources() {
		return transportService.uploadFileByLocalDirectory(transportService.makeFtpDirectory(FileTypeEnum.UPLOAD),
				transportService.makeLocalFileDirectory());
	}

	@ResponseBody
	@RequestMapping("/uploadFile")
	public R uploadFile(String localFilPath) {
		return transportService.uploadFileByLocalPath(transportService.makeFtpDirectory(FileTypeEnum.UPLOAD),
				localFilPath);
	}

	@ResponseBody
	@RequestMapping("/uploadFileByDB")
	public R uploadFileByDB() {
		return transportService.uploadFileByDB(transportService.makeFtpDirectory(FileTypeEnum.UPLOAD),
				ftpServerInfo.getLocalDirectory(), transportService.makeFtpDirectory(FileTypeEnum.DOWNLOAD));
	}

	@ResponseBody
	@RequestMapping("/saveDefinitionFileInfo")
	public R saveDefinitionFileInfo() {
		return transportService.saveDefinitionFileInfo(transportService.makeFtpPathByDifiniton());
	}
}
